package com.hll.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.hll.model.TCoupon;
import com.hll.model.TUser;
import com.hll.model.query.TCouponExample;
import com.hll.model.query.TUserExample;
import com.hll.service.TCouponService;
import com.hll.service.TUserService;
import com.hll.util.JsonUtils;
import com.hll.util.SmsUtils;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Api(value = "user", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private TCouponService tCouponService;

    @Autowired
    private TUserService tUserService;

    Logger logger = Logger.getLogger(LoginController.class);

    private static String trmotoUrl = "http://182.92.119.224:8999/trmoto";

    /**
     * I1.0 -- 点击登录按钮
     *
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/submit", method = {RequestMethod.GET, RequestMethod.POST})
    public Object pageList(String json, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String mobile = "";
        String code = "";
        Map<String, Object> paramMap = JsonUtils.toObject(json, Map.class);
        if (null != paramMap.get("mobile")) {
            mobile = paramMap.get("mobile").toString();
        }
        if (null != paramMap.get("code")) {
            code = paramMap.get("code").toString();
        }
        //判断手机号是否注册，若没有注册直接创建该用户的账户
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUserNameEqualTo(mobile);
        List<TUser> lst = this.tUserService.selectByExample(tUserExample);
        Long userid = 0l;
        //如果手机号没有注册过，则直接注册
        if (lst.size() == 0) {
            TUser tUser = new TUser();
            tUser.setUserName(mobile);
            tUser.setCreateTime(new Date());
            tUser.setModifyTime(new Date());
            this.tUserService.insertSelective(tUser);
            userid = tUser.getId();
        } else {
            userid = lst.get(0).getId();
        }
        result.put("userid", userid);
        result.put("mobile", mobile);
        result.put("msg_code", "0000");
        result.put("msg_desc", "success");
        return result;
    }


    /**
     * I2.0 -- 生成校验码
     *
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/code", method = {RequestMethod.GET, RequestMethod.POST})
    public Object code(String json, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String mobile = "";
        String code = "" + (int) ((Math.random() * 9 + 1) * 1000);
        logger.info(code);

        Map<String, Object> paramMap = JsonUtils.toObject(json, Map.class);
        try {
            if (null != paramMap.get("mobile")) {
                mobile = paramMap.get("mobile").toString();
            }
            Map<String, Object> map = new HashMap<String, Object>();
            String message = "【腾瑞明】验证码" + code + "，请在10分钟内完成验证，感谢您的使用。";
            String msg = SmsUtils.sendSMS(mobile, message);
            if("0".equals(msg)&&null!=msg){
                result.put("code", code);
                result.put("msg_code", "0000");
                result.put("msg_desc", "success");
            }else {
                result.put("msg_code", "9991");
                result.put("msg_desc", "send_error");
            }
        } catch (Exception e) {
            result.put("msg_code", "9999");
            result.put("msg_desc", "server_error");

        }
        return result;
    }





}
